package com.siyougame.business.service.template;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.siyougame.business.controller.admin.templateItem.vo.TemplateItemCreateReqVO;
import com.siyougame.business.controller.app.vo.AppTemplatePageReqVO;
import com.siyougame.business.convert.templateItem.TemplateItemConvert;
import com.siyougame.business.dal.dataobject.templateItem.TemplateItemDO;
import com.siyougame.business.dal.dataobject.user.WxUserDO;
import com.siyougame.business.enums.TemplateItemStatusEnum;
import com.siyougame.business.enums.TemplateStatusEnum;
import com.siyougame.business.service.templateItem.TemplateItemService;
import com.siyougame.business.service.user.WxUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.siyougame.business.controller.admin.template.vo.*;
import com.siyougame.business.dal.dataobject.template.TemplateDO;
import com.siyougame.common.pojo.PageResult;

import com.siyougame.business.convert.template.TemplateConvert;
import com.siyougame.business.dal.mysql.template.TemplateMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import static com.siyougame.common.exception.util.ServiceExceptionUtil.exception;
import static com.siyougame.business.enums.ErrorCodeConstants.*;

/**
 * 模板 Service 实现类
 *
 * @author ${table.author}
 */
@Service
@Validated
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper,  TemplateDO> implements TemplateService {

    @Resource
    private TemplateMapper templateMapper;
    @Resource
    private TemplateItemService templateItemService;
    @Resource
    private WxUserService wxUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createTemplate(TemplateCreateReqVO createReqVO) {
        // 插入
        TemplateDO template = TemplateConvert.INSTANCE.convert(createReqVO);
        template.setStatus(TemplateStatusEnum.NORMAL.getCode());
        templateMapper.insert(template);

        //插入明细
        if(CollectionUtil.isNotEmpty(createReqVO.getTemplateItems())){
            for (TemplateItemCreateReqVO item : createReqVO.getTemplateItems()){
                TemplateItemDO templateItem = TemplateItemConvert.INSTANCE.convert(item);
                templateItem.setTempId(template.getId());
                templateItem.setStatus(TemplateItemStatusEnum.NORMAL.getCode());
                templateItemService.save(templateItem);
            }
        }

        // 返回
        return template.getId();
    }

    @Override
    public void updateTemplate(TemplateUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTemplateExists(updateReqVO.getId());
        // 更新
        TemplateDO updateObj = TemplateConvert.INSTANCE.convert(updateReqVO);
        templateMapper.updateById(updateObj);
    }

    @Override
    public void deleteTemplate(Long id) {
        // 校验存在
        this.validateTemplateExists(id);
        // 删除
        this.update(Wrappers.lambdaUpdate(TemplateDO.class)
                .set(TemplateDO::getStatus, TemplateStatusEnum.DELETED.getCode())
                .eq(TemplateDO::getId, id));
    }

    private void validateTemplateExists(Long id) {
        if (templateMapper.selectById(id) == null) {
            throw exception(TEMPLATE_NOT_EXISTS);
        }
    }

    @Override
    public TemplateDO getTemplate(Long id) {
        return templateMapper.selectById(id);
    }

    @Override
    public List<TemplateDO> getTemplateList(Collection<Long> ids) {
        return templateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TemplateDO> getTemplatePage(TemplatePageReqVO pageReqVO) {
        return templateMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<TemplateDO> getTemplatePage(AppTemplatePageReqVO pageReqVO) {
        Object loginId = StpUtil.getLoginId();
        WxUserDO wxUserDO = wxUserService.getById(String.valueOf(StpUtil.getLoginId()));
        pageReqVO.setDepartmentId(wxUserDO.getThirdDepartmentId());
        return templateMapper.selectPage(pageReqVO);
    }
    //
    //@Override
    //public List<TemplateDO> getTemplateList(TemplateExportReqVO exportReqVO) {
    //    return templateMapper.selectList(exportReqVO);
    //}

}
